package com.huluspace.learnhub.dto;

import com.huluspace.learnhub.model.Content;
import lombok.Data;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

@Data
public class ContentDTO {
  private Long id;
  private String type;
  private String title;
  private List<String> tags;
  private String sourceUrl;
  private Boolean isTop;
  private String author;
  private String body;
  private Integer views;
  private Integer likes;
  private LocalDateTime publishTime;

  public static ContentDTO fromEntity(Content content) {
    ContentDTO dto = new ContentDTO();
    dto.setId(content.getId());
    dto.setType(content.getType());
    dto.setTitle(content.getTitle());
    if(content.getTags() != null)
      dto.setTags(Arrays.stream(content.getTags().split(",")).toList());
    dto.setSourceUrl(content.getSourceUrl());
    dto.setIsTop(content.getIsTop());
    dto.setPublishTime(content.getCreatedAt());
    dto.setAuthor(content.getAuthor().getUsername());
    dto.setBody(content.getBody());
    dto.setViews(content.getViewCount());
    dto.setLikes(content.getLikeCount());
    return dto;
  }
}

